﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoadArrow : MonoBehaviour
{
	public Transform head;
	public Transform lineParent;
	public Renderer lineMesh;
	private Vector3 end;
	private Vector3 start;

	
	private void Awake()
	{
		lineMesh.transform.localScale = new Vector3(0, 1, 1);
	}
	public void SetStart(float x,float y,float z)
	{
		lineParent.position = new Vector3(x,y,z);
	}
	public void SetEnd(float x,float y,float z)
	{
		head.position = new Vector3(x, y, z);
	}
	public void UpdateLine()
	{
		var dir = head.position - lineParent.position;
		float dis = dir.magnitude;
		lineMesh.transform .localScale = new Vector3(dis, 1, 1);
		lineMesh.material.SetTextureScale("_MainTex", new Vector2(dis, 1));
		lineParent.forward = dir;
		head.forward = dir;
	}
#if UNITY_EDITOR
	void Update()
    {
		if (head == null) return;
        if(head.position != end)
		{
			end = head.position;
			UpdateLine();
		}
		if(lineParent.position != start)
		{
			start = lineParent.position;
			UpdateLine();
		}
    }
#endif
}
